<?php
namespace Core\Libs\Message;

class Message {

	private static $arrayOfMessages = array('default' => array('send' => array(), 'receive' => array()));
	private static $arrayTypesOfMessages = array('success', 'message', 'notice', 'alert', 'error', 'warning');
	private static $isDevelopment = FALSE;
	private static $isDebug = FALSE;
	private $current = array();
	private $id = 'default';
	private $type = 'send';

	public function __construct($id = NULL, $type = 'send') {

		if(empty($id)) {

			$this->id = \Core\Libs\MasterController\MasterController::getRequest()->getName();
		}

		else {

			$this->id = $id;
		}

		if(empty($type)) {

			$this->type = 'send';
		}

		else {

			$this->type = $type;
		}

		self::$isDevelopment = \Core\Uses\Ler('Core.development');
		self::$isDebug = \Core\Uses\Ler('Core.debug');
		$this->current = array();
	}

	public function __destruct() {

		unset($this, $this->current);
	}

	final public function getId() {

		return $this->id;
	}

	final public function getType() {

		return $this->type;
	}

	final public function add($id = NULL, $value = NULL, $options = array()) {

		if(!empty($id)) {

			$type = array_search($options['type'], self::$arrayTypesOfMessages);
	
			if(empty($type) || $type === FALSE) {
	
				$type = 'notice';
			}

			if(!is_array($options)) {

				$options = array('type' => $type);
			}

			else {

				$options['type'] = $type;
			}

			$development = self::$isDevelopment;
			$arrayData = array_merge(array('type' => 'notice', 'code' => NULL, 'severityCode' => NULL, 'severityName' => NULL, 'internal' => NULL, 'file' => NULL, 'line' => NULL, 'debugBackTrace' => NULL), $options);

			if($development) {

				$debug = self::$isDebug;

				if(empty($options['internal'])) {

					$arrayData['internal'] = \Core\Uses\getInternalError();
				}

				if(empty($options['file'])) {

					$arrayData['file'] = \Core\Uses\getErrorFile();
				}

				if(empty($options['line'])) {

					$arrayData['line'] = \Core\Uses\getErrorLine();
				}

				if(array_key_exists('debugBackTrace', &$options) && empty($options['debugBackTrace'])) {

					$arrayData['debugBackTrace'] = debug_backtrace(FALSE);
				}
			}

			if(empty($options['code']) && $type !== 'success') {

				$arrayData['code'] = \Core\Uses\Exception()->getCode();
			}

			if(empty($options['severityCode']) && $type !== 'success') {

				$arrayData['severityCode'] = \Core\Uses\getSeverity();
			}

			if(empty($options['severityName']) && $type !== 'success') {

				$arrayData['severityName'] = \Core\Uses\getSeverityAsString();
			}

			$isForm = \Core\Libs\MasterObject\MasterObject::getRequest()->getElement(array());

			if(self) {

				
			}

			self::$arrayOfMessages[$this->id][$this->type][$id] = array('data' => $value, 'options' => $arrayData);
		}

		return $this;
	}

	final public function reset() {

		self::$arrayOfMessages = array();
		return $this;
	}

	final public function remove() {

		self::$arrayOfMessages[$this->id][$this->type] = array();
		return $this;
	}

	final public function removeElement($element = NULL) {

		if(!empty($element)) {

			$current = $this->current[$this->type];

			if(is_array($current) && array_key_exists($element, $current)) {

				unset(self::$arrayOfMessages[$this->id][$this->type][$element], $current);
				//self::$arrayOfMessages[$this->id][$this->type] =& array_values(self::$arrayOfMessages[$this->id][$this->type]);
			}
		}

		return $this;
	}

	final public function getAll() {

		return self::$arrayOfMessages;
	}

	final public function get() {

		if(is_array(self::$arrayOfMessages[$this->id][$this->type]) && count(self::$arrayOfMessages[$this->id][$this->type]) > 0) {

			$this->current = end(self::$arrayOfMessages[$this->id]);
		}

		return $this;
	}

	final public function getElement($element = NULL) {

		if(!empty($element)) {

			$current = $this->current[$this->type];

			if(is_array($current) && array_key_exists($element, $current)) {

				unset($current);
				return $this->current[$element];
			}

			unset($current);
		}

		return NULL;
	}

	final public function getSent() {

		return $this->current['send'];
	}

	final public function getReceived() {

		return $this->current['receive'];
	}

	final public function setMessage($message, $send = FALSE, $options = array(), $type = 'send') {

		if($type !== 'send') {

			$type = 'receive';
		}

		self::$arrayOfMessages[$this->id]['messages'][$type] = $message;

		if($send) {

			if(is_array($options)) {

				if(count($options) < 1) {

					$options = array('json', 'options' => NULL);
				}
			}

			else {

				$options = array($options, 'options' => NULL);
			}

			self::$arrayOfMessages[$this->id]['options'] = $options;
			$this->send();
		}

		else {

			return $this;
		}
	}

	final public function addMessage($message = NULL, $send = FALSE, $options = array(), $type = 'send') {

		if($type !== 'send') {

			$type = 'receive';
		}

		if(is_array($message)) {

			foreach($message as $i => $value) {

				self::$arrayOfMessages[$this->id][$type][$i] = $value;
			}
		}

		else {

			self::$arrayOfMessages[$this->id][$type][] = $message;
		}

		if($send && $type === 'send') {

			$this->send($options);
		}

		else {

			return $this;
		}
	}

	final public function receive() {

		$message =& func_get_arg(0);
		$options =& func_get_arg(1);
		$isReturn = FALSE;

		if(!is_array($message)) {

			$message = array();
		}

		if(is_array($options)) {

			if(count($options) < 1) {

				$options = array('json', 'options' => NULL);
			}
		}

		else {

			$options = array($options, 'options' => NULL);
		}

		if(in_array('json', $options)) {

			try {

				self::$arrayOfMessages[$this->id]['messages']['receive'] = json_decode(array_merge($message, array('_[jabez-php]' => array('development' => \Core\Uses\Ler('Core.development'), 'debug' => \Core\Uses\Ler('Core.debug'))), TRUE), $options['options']);
			}

			catch(\Exception $e) {

				\Core\Uses\setSeverity(100);
				self::$arrayOfMessages[$this->id]['messages']['receive'] = array('_[jabez-php]' => array('development' => \Core\Uses\Ler('Core.development'), 'debug' => \Core\Uses\Ler('Core.debug'), 'message' => 'Erro: Problema ao codificar a mensagem para envio com JSON.', 'type' => 'error', 'code' => json_last_error(), 'severity' => \Core\Uses\getSeverityAsString()));
			}
		}
	}
}

class Ajax extends Message {

	private $jsonConfig = NULL;

	final public function setJsonConfig($jsonConfig = NULL) {

		$this->jsonConfig = $jsonConfig;
		return $this;
	}

	final public function getJsonConfig() {

		return $this->jsonConfig;
	}

	public function __construct($id = NULL, $type = 'send', $jsonConfig = NULL) {

		parent::__construct($id, $type);
		$this->jsonConfig = $jsonConfig;
	}

	final public function send() {

		$options =& func_get_arg(0);
		$isReturn = FALSE;

		if(!is_array($options)) {

			$options = array($options);
		}

		$dataType = \Core\Libs\MasterObject\MasterObject::getRequest()->getElement(array('ajaxRequest', 'dataType'));

		if(!empty($dataType)) {

			$options[] = $dataType;
		}

		if(in_array('json', $options)) {

			try {

				self::$arrayOfMessages[$this->id]['send'] = json_encode(self::$arrayOfMessages[$this->id]['send'], $this->jsonConfig);
			}

			catch(\Exception $e) {

				\Core\Uses\setSeverity(100);
				self::$arrayOfMessages[$this->id]['send'] = array('_jabezPHP' => array('data' => 'Erro: Problema ao codificar a mensagem para envio com JSON.', 'options' => array('development' => self::$isDevelopment, 'debug' => self::$isDebug, 'type' => 'error', 'internal' => \Core\Uses\getInternalError(), 'code' => json_last_error(), 'severityCode' => \Core\Uses\getSeverity(), 'severityName' => \Core\Uses\getSeverityAsString())));
			}
		}

		if(in_array('return', $options)) {

			$isReturn = TRUE;
		}

		if($isReturn) {

			return self::$arrayOfMessages[$this->id]['send'];
		}

		echo(self::$arrayOfMessages[$this->id]['send']);
	}
}

class WebService extends Message {

	
}
?>